<template>
  <div id="login">
    <div class="login">
      <div class="logo">
        <img src="../assets/logo.jpg"/>
        <span>MK System</span>
      </div>
      <!--      表单-->
      <div class="form">
        <el-form @keydown.enter="submitForm('ruleForm')" :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="账号: " prop="checkPass" :error="nameErrorMsg">
            <el-input  clearable type="text" @blur="verifyName" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码: " prop="pass">
            <el-input  clearable type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="验证码:" :inline="true" prop="identity">
            <el-row>
              <el-col :span="11" >
                <el-input type="text"  clearable @keydown.enter="submitForm('ruleForm')" v-model="ruleForm.identity"  autocomplete="off"></el-input>
              </el-col>
              <el-col :span="9" :offset="2" class="identity">
                <Identity ref="myIdentity" :identifyCode="identifyCode" @refresh-code="refreshCode(identifyCodes,4)"></Identity>
              </el-col>
            </el-row>
            
          </el-form-item>
          <el-form-item align="strech">
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
          
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="less">
</style>
<style lang="less" scoped>
  #login {
    height: 100%;
    .login {
      width: 360px;
      height: 320px;
      background-color: rgba(0,0,0,.1);
      border: 1px solid white;
      border-radius: 5px;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      margin: auto;
      padding: 30px;
      .logo {
        line-height: 50px;
        text-align: center;
        border-bottom: 1px solid rgba(255,255,255,.3);
        padding-bottom: 10px;
        margin-bottom: 10px;
        img {
          width: 50px;
          border-radius: 50%;
          vertical-align: middle;
          margin: 0 20px;
        }
        span {
          color: white;
        }
      }
      .el-form {
        width: auto;
        .identity {
        
        }
      }
    }
  }
</style>
<script>
  import * as url from '../api/cfApi'
  import Identity from '../components/common/Identity';
  export default {
    name: 'Login',
    components: {
      Identity
    },
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'))
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass')
          }
          callback()
        }
      }
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'))
        } else
          callback()
      }
      var validateIdentity = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入验证码'))
        } else if (value !== this.identifyCode) {
          this.refreshCode(this.identifyCodes,4)
          callback(new Error('请输入正确的验证码'))
        } else callback()
      }
      return {
        nameErrorMsg: '',
        identifyCode: '',
        identifyCodes: '1234567890',
        ruleForm: {
          pass: '',
          checkPass: '',
          identity: '',
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          identity: [
            { validator: validateIdentity, trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      verifyName(formName){
        if (this.ruleForm.checkPass !== '') {
          console.log('验证码login是否存在')
          this.nameErrorMsg = ''
          this.$store.dispatch('verifyNumberAC', {number: this.ruleForm.checkPass}).then(res=>{
            if (res.message=='该账号不存在') {
               this.nameErrorMsg=res.message
            }
          })
        }
      },
      submitForm (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // TODO：登录功能
            const obj = {
              number: this.ruleForm.checkPass,
              password: this.ruleForm.pass,
            }
            this.$store.dispatch('loginAc',obj).then(res => {
              console.log(res)
              if (res.code === '500') {
                this.$message.error('登录失败,请重试')
                this.$refs.myIdentity.drawPic()
              }
              else if (res.code === '200') {
                this.$router.push('/index')
              } else if (res.code === '500') this.$message.error('密码错误请重试')
            })
          } else {
            // console.log('error submit!!')
            // this.$message.error('y')
            return false
          }
        })
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
      },
      randomNum(min, max) {   // 产生随机数
        return Math.floor(Math.random() * (max - min) + min);
      },
      refreshCode() {
        this.identifyCode = "";
        this.makeCode(this.identifyCodes, 4);
      },
      makeCode(o, l) {
        for (let i = 0; i < l; i++) {
          this.identifyCode += this.identifyCodes[
            this.randomNum(0, this.identifyCodes.length)
            ];
        }
        console.log(this.identifyCode);
      }
    },
    created() {
      this.makeCode(this.identifyCodes, 4)
    }
  }
</script>
